﻿#region Copyright & License
//
// Project: Quark Framework
// Project site: http://quark.codeplex.com
// See the copyright statement on the project site for more details.
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Quark.Framework.Exceptions;

namespace Quark.Framework
{
    /// <summary>
    /// 实现<see cref="IQuarkComponent"/>接口的通用组件基类
    /// </summary>
    public abstract class QuarkComponentBase : IQuarkComponent
    {
        #region Members

        /// <summary>
        /// 当前组件实例唯一ID
        /// </summary>
        private Guid _componentInstanceIdentifier;
        /// <summary>
        /// 当前组件实例被置入的上下文
        /// </summary>
        private IQuarkComponentContext _context;

        #endregion

        #region Constructors

        /// <summary />
        protected QuarkComponentBase()
        {
            this._componentInstanceIdentifier = Guid.NewGuid();
        }

        #endregion

        #region Context

        /// <summary>
        /// 获取当前组件实例被置入的上下文，
        /// 如果当前组件实例未进行过置入上下文，即未被调用<see cref="IContextualizable.Contextualize(IQuarkContext)"/>方法，
        /// 则本属性返回null空值
        /// </summary>
        protected IQuarkComponentContext Context
        {
            get
            {
                return this._context;
            }
        }

        #endregion

        #region IQuarkComponent 成员

        /// <summary>
        /// 获取当前组件实例的唯一实例ID
        /// </summary>
        /// <seealso cref="IQuarkComponent.ComponentInstanceId"/>
        public virtual Guid ComponentInstanceId
        {
            get
            {
                return this._componentInstanceIdentifier;
            }
            protected set
            {
                this._componentInstanceIdentifier = value;
            }
        }

        #endregion

        #region IIdentifiable 成员

        /// <summary>
        /// 获取当前对象实例的唯一实例标识
        /// </summary>
        /// <seealso cref="IIdentifiable.InstanceIdentifier"/>
        public object InstanceIdentifier
        {
            get
            {
                return this.ComponentInstanceId;
            }
        }

        #endregion

        #region IContextualizable<IQuarkComponentContext> 成员

        /// <summary>
        /// 当前对象实例置入上下文环境
        /// </summary>
        /// <seealso cref="IContextualizable{T}.Contextualize(T)"/>
        public virtual void Contextualize(IQuarkComponentContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            this._context = context;
        }

        #endregion

        #region IContextualizable 成员

        /// <summary>
        /// 当前对象实例置入上下文环境
        /// </summary>
        /// <seealso cref="IContextualizable.Contextualize(IQuarkContext)"/>
        public void Contextualize(IQuarkContext context)
        {
            this.Contextualize(context == null ? null : context as IQuarkComponentContext);
        }

        #endregion

        #region IDisposable 成员

        /// <summary>
        /// 执行与释放或重置非托管资源相关的应用程序定义的任务
        /// </summary>
        /// <seealso cref="IDisposable.Dispose()"/>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary/>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
            }
        }

        #endregion
    }
}
